package com.feeyo.groundservice.composewidget.usage

import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerDefaults
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import com.feeyo.groundservice.base_util.FyLog
import com.feeyo.groundservice.base_util.delayMillToExecute
import com.feeyo.groundservice.composewidget.R
import com.feeyo.groundservice.composewidget.usage.base.BaseComposeActivity
import com.feeyo.groundservice.composewidget.util.dimenToDp
import com.feeyo.groundservice.composewidget.util.toComposeColor
import com.feeyo.groundservice.composewidget.view.compose_pager.ComposePager
import com.feeyo.groundservice.composewidget.view.compose_pager.ComposePagerState
import com.feeyo.groundservice.composewidget.view.compose_pager.IPageChange
import com.feeyo.groundservice.composewidget.view.image.CustomAsyncImage
import com.feeyo.groundservice.composewidget.view.slidebar.GuaranteeNodeSlideBar

/**
 *作者：QLH on 2023-08-05
 *描述：
 */
class ComposePagerActivity : BaseComposeActivity() {

    private val images = mutableStateListOf(
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F66b7ce397068c1f4710cafe4e1827ab5f7565180.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665065304&t=c45a94bbac62a3bd502dc53e40afc583",
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2Fc0793b2877f09ded49e96e3b3e05781d4f1e2e9e.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665065303&t=86940ecb5bf20d1c90b5fb1c1f0afb06",
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F79c593c97cb1aef62160a7c6165ea3ecdc60f064.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665065301&t=0d840bcfe779bcac4ea65db6d257c417",
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F2726b76584c11dc75449024ad6105893be1edd0f.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665065300&t=7afae3a8eccb498fb37d54ff54d2016b",
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            Column {
                horizontalPage(Modifier.fillMaxWidth().weight(1f))
                verticalPage(Modifier.fillMaxWidth().weight(1f))
                SystemPage(Modifier.fillMaxWidth().weight(1f))
                GuaranteeNode()
            }
        }
    }

    @Composable
    private fun verticalPage(modifier: Modifier) {
        val composePagerState = remember { ComposePagerState() }
        ComposePager(
            pageCount = images.size,
            modifier = modifier,
            composePagerState = composePagerState,
            orientation = Orientation.Vertical,
            pageCache = 2
        ) {
            CustomAsyncImage(
                model = images[index],
                contentDescription = null,
                contentScale = ContentScale.FillBounds
            )
        }
    }

    @Composable
    private fun horizontalPage(modifier: Modifier) {
        val composePagerState = remember { ComposePagerState() }
        ComposePager(
            pageCount = images.size,
            modifier = modifier,
            composePagerState = composePagerState,
            orientation = Orientation.Horizontal,
            pageCache = 2,
            pageChange = object :IPageChange{
                override fun onNextPage() {
                    Log.e("ComposePager","onNextPage")
                }

                override fun onPrePage() {
                    Log.e("ComposePager","onPrePage")
                }
            }
        ) {
            CustomAsyncImage(
                model = images[index],
                contentDescription = null,
                contentScale = ContentScale.FillBounds
            )
        }
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    private fun SystemPage(modifier: Modifier) {
        val pagerState = rememberPagerState(1, pageCount = { 2 })//初始化页面，0就表示第一个页面

        HorizontalPager(
            state = pagerState,
            modifier = modifier,
            flingBehavior = PagerDefaults.flingBehavior(pagerState, snapPositionalThreshold = 0.8f)
        ) {
            val color =
                if (it == 0)
                    R.color.red_EB5451.toComposeColor()
                else
                    Color.Green
            Box(
                modifier = Modifier.fillMaxSize()
                    .background(color = color)
            )
        }

        LaunchedEffect(key1 = pagerState.currentPage) {
            Log.e("xxxxx", "currentPage:${pagerState.currentPage}")
        }
    }


    @Composable
    private fun GuaranteeNode() {
        Box(
            modifier = Modifier.size(
                R.dimen.d200.dimenToDp(),
                R.dimen.d40.dimenToDp()
            ),
            contentAlignment = Alignment.Center
        ) {
            var resume = {}
            GuaranteeNodeSlideBar(
                isBorder = true,
                borderColor = listOf(
                    R.color.red_EB5451.toComposeColor(),
                    R.color.green_00B578.toComposeColor()
                ),
                onCurrentPageIndex = {
                    FyLog.e("GuaranteeNodeSlideBar", "currentPage:$it")
                    delayMillToExecute {
                        resume.invoke()
                    }
                },
                onResumeIndex = {
                    resume = it
                }
            )

            Row(
                modifier = Modifier.fillMaxWidth().height(R.dimen.d45.dimenToDp())
                    .background(Color.Transparent),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = "GuaranteeNode",
                )
            }
        }
    }
}